package com.wica.view;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;

import com.wica.adapter.ELAMostViewed;
import com.wica.facebook.FBRocketConnect;
import com.wica.facebook.Facebook;
import com.wica.facetube.FaceTubeService;
import com.wica.facetube.R;
import com.wica.view.tab_preference.TabPreferencesMostViewed;

public class ELMostViewed extends EfficientList implements OnSharedPreferenceChangeListener{	
	// *************** Constants ************** //  
	// Tab settings constants
    public static final String SETTINGS_1 = "most_viewed_time";
    public static final String SETTINGS_2= "most_viewed_numberOfResults";
    // MENU options constants
	private static final int MENU_SET_MOOD = Menu.FIRST;
	private static final int MENU_SET_TEXTLOCATION = Menu.FIRST+1;
	private static final int MENU_SETTINGS = Menu.FIRST+2;	
	private static final int LOG_OFF_FACEBOOK = Menu.FIRST+3;
	private static final int REQUESTCODE_FACEBOOK = 1;	
	
	// *************** Methods ************** //
	@Override
	public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Toast.makeText(this, "Press MENU button to get more options.", Toast.LENGTH_LONG).show();
        setListAdapter(new ELAMostViewed(this));
        PreferenceManager.getDefaultSharedPreferences(this).registerOnSharedPreferenceChangeListener(this);
    }// onCreate method

	@Override
	public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,String key) {
		if (key.equals(SETTINGS_1)) {
	        setListAdapter(new ELAMostViewed(this));
        }else if (key.equals(SETTINGS_2)) {
	        setListAdapter(new ELAMostViewed(this));
        }		
	}// onSharedPreferenceChanged method   
	
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);
        // Build the menu shown when we hit menu button (KeyEvent class - KEYCODE_MENU) while playing the video.
        menu.add(0, MENU_SET_MOOD, 1, R.string.set_mood)
				.setShortcut('0', 'r')
				.setIcon(R.drawable.stat_happy);
        menu.add(0, MENU_SET_TEXTLOCATION, 1, R.string.set_textLocation)
				.setShortcut('0', 'r')
				.setIcon(R.drawable.menu_location_by_text);
        menu.add(0, MENU_SETTINGS, 2, R.string.settings)
        		.setShortcut('0', 'r')
        		.setIcon(R.drawable.ic_settings);
//        menu.add(0, LOG_OFF_FACEBOOK, 2, "Sign out")
//				.setShortcut('0', 'r')
//				.setIcon(R.drawable.log_off);
        return true;
    }// onCreateOptionsMenu method    

	@Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle all of the possible menu actions.
        switch (item.getItemId()) {
        case MENU_SET_MOOD:
        	this.startActivity(new Intent(this, MoodDialog.class));
            break;
    	case MENU_SET_TEXTLOCATION:
    		this.startActivity(new Intent(this, TextLocationDialog.class));
    		break;
		case MENU_SETTINGS:
			this.startActivity(new Intent(this, TabPreferencesMostViewed.class));
			break;
//		case LOG_OFF_FACEBOOK:
//        	Editor s = PreferenceManager.getDefaultSharedPreferences(this).edit();
//        	s.remove("userID");
//        	s.commit();
//        	Log.d(LOG_TAG,"sign out");
//    		this.startActivityForResult(new Intent(this,FBRocketConnect.class),REQUESTCODE_FACEBOOK);
//        	break;
    	}
        return super.onOptionsItemSelected(item);
    }//onOptionsItemSelected method
    
	@Override
	protected void  onActivityResult(int requestCode, int resultCode, Intent data){
       	Editor s;
		Bundle bundle;
		Long facebookID;
		FaceTubeService facetube;
		Long userID;
		Log.v(LOG_TAG,"onactivityresult("+requestCode+")");
			bundle = data.getExtras();
   			facebookID=(Long)bundle.get("userID");
  			facetube = new FaceTubeService();
  			userID = facetube.faceTubeFacebookID2userID(facebookID);
   			s = PreferenceManager.getDefaultSharedPreferences(this).edit();
   			s.putInt("userID",Integer.valueOf(userID.toString()));
   			s.commit();
   			Log.v(LOG_TAG,"INITIAL CONFIG");
   			Intent intent = new Intent(this,InitialConfiguration.class);
   			this.startActivity(intent);
    }//onActivityResult method

	@Override
	protected String getListTimeSetting() {
		return PreferenceManager.getDefaultSharedPreferences((Context)this).getString("most_viewed_time", "today");
	}
	
	
}// EfficientListMostViewed class
